package com.fzrskj.cloud.free.dic.utils;

import com.fzrskj.cloud.free.dic.bean.DicParam;
import com.fzrskj.cloud.free.dic.dao.DicParamDao;
import com.fzrskj.cloud.free.user.bean.UserAccountView;
import com.fzrskj.cloud.free.user.utils.UserAccountUtils;
import ms.core.object.CObject;
import ms.core.tool.StrTool;

import java.util.List;
import java.util.Map;

public class DicParamUtils extends CObject{
	/**
	 * 更新以及添加系统配置表
	 * @param dicParamList
	 * @return
	 */
	//如果跟新数据错误，跳出for循环返回错误代码
	public int uptListDicParam(List<DicParam> dicParamList, int paramType){
		int update = 0;
		DicParamDao dao = new DicParamDao();
		for(int i = 0; i<dicParamList.size();i++){
			if (StrTool.isBlankStr(dicParamList.get(i).getParamName())) {
				continue;
			}
			if(dao.getParamByNameAndType(dicParamList.get(i).getParamName(), paramType) == null){
				update =  dao.addDicParam(dicParamList.get(i).getParamName(), dicParamList.get(i).getParamValue(), paramType);
			}else{
				update = dao.uptDicParam(dicParamList.get(i).getParamValue() ,dicParamList.get(i).getParamName(), dicParamList.get(i).getParamType());
			}
		}
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return update;
	}

	/**
	 * 获取系统配置中的参数
	 * @param paramType parameter type.
	 * @return list of DicParam
	 */
	public List<DicParam> getListDicParam(int paramType) {
		return  this.getListDicParam(paramType, null);
	}

	/**
	 * 获取系统配置中的参数
	 * @param paramType parameter type.
	 * @param startFrom start from likes.
	 * @return list of DicParam.
	 */
	public List<DicParam> getListDicParam(int paramType, String startFrom) {
		DicParamDao dao = new DicParamDao();
		List<DicParam> list = dao.getListDicParam(paramType, startFrom);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return list;
	}

	/**
	 * 更新系统配置参数
	 * @param paramValue
	 * @param paramName
	 * @param paramType
	 * @return
	 */
	public int uptDicParam(String paramValue,String paramName,int paramType) {
		DicParamDao dao = new DicParamDao();
		int i = dao.uptDicParam(paramValue, paramName, paramType);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}

	/**
	 * 批量添加/修改操作
	 * @param dicParamList
	 * @param paramType
	 * @return
	 */
	public int addListDicParam(List<DicParam> dicParamList,int paramType) {
		int add = 0;
		DicParamDao dao = new DicParamDao();
		for(int i=0;i<dicParamList.size();i++){
			if((i==0&&add==0)||add >0){
				if(dao.getParamByNameAndType(dicParamList.get(i).getParamName(), paramType) == null){
					add =  dao.addDicParam(dicParamList.get(i).getParamName(), dicParamList.get(i).getParamValue(), paramType);
				}else{
					add =dao.uptDicParam(dicParamList.get(i).getParamValue(), dicParamList.get(i).getParamName(),paramType);
				}
			}else{
				break;
			}
		}
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return add;
	}
	/**
	 * 通过name与属性查询数值
	 * @param paramName
	 * @param paramType
	 * @return
	 */
	public String getParamValue(String paramName, int paramType){
		DicParamDao dao = new DicParamDao();
		String str = dao.getParamValue(paramName, paramType);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return str;
	}

	/**
	 * get parameter value by parameter name
	 * @param paramName
	 * @return
	 */
	public static String getParamValue(String paramName){
		DicParamDao dao = new DicParamDao();
		String str = dao.getParamValue(paramName);
		dao.dispose();
		return str;
	}

	/**
	 * get parameter value by parameter name.
	 * @param paramName
	 * @param agentId
	 * @return
	 */
	public static String getParamValue(String paramName, Integer agentId){
		DicParamDao dao = new DicParamDao();
		String str = dao.getParamValue(paramName, agentId);
		dao.dispose();
		return str;
	}

	/**
	 * get parameter value by parameter name
	 * @param paramName
	 * @return
	 */
	public static String getParamValueByAgentIdAndParamName(String paramName, Integer agentId){
		DicParamDao dao = new DicParamDao();
		String str = dao.getParamValueByAgentIdAndParamName(paramName, agentId);
		dao.dispose();
		return str;
	}

	/**
	 * 获取最近的使用自签支付渠道的代理商ID.
	 * @param paramName 渠道名.
	 * @param currentAgentId 当前级别代理商ID.
	 * @return
	 */
	public static Integer getLastSelfPayParent(String paramName, Integer currentAgentId) {
		if (currentAgentId == 0) {
			return currentAgentId;
		}

		UserAccountView accountView = UserAccountUtils.getUserAccountWithoutAgent(currentAgentId);
		String way = getParamValue(paramName, accountView.getAgentId());
		if ("1".equals(way)) {
			return accountView.getAgentId();
		}
		return getLastSelfPayParent(paramName, accountView.getAgentId());
	}



	/**
	 * 更新系统配置参数
	 * @param paramValue
	 * @param paramName
	 * @param paramType
	 * @return
	 */
	public int uptDicParam(String paramValue,String paramName,int paramType, boolean isUseAgentId) {
		DicParamDao dao = new DicParamDao();
		int i = dao.uptDicParam(paramValue, paramName, paramType, isUseAgentId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}

	/**
	 * 根据paramName跟paramType查询对象
	 * @param paramName
	 * @param paramType
	 * @return
	 */
	public DicParam getParamByNameAndType(String paramName,int paramType){
		DicParamDao dao = new DicParamDao();
		DicParam dicParam = dao.getParamByNameAndType(paramName, paramType);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return dicParam;
	}

	public int addDicParam(Map<String, Object> values){
		DicParamDao dao = new DicParamDao();
		int i = dao.addDicParam(values);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}

	public int newDicParam(Map<String, Object> values){
		DicParamDao dao = new DicParamDao();
		int i = dao.newDicParam(values);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}
	
	public int delDicParam(int id){
		DicParamDao dao = new DicParamDao();
		int i = dao.delDicParam(id);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}
	
	public int delDicParam(Map<String, Object> cdt){
		DicParamDao dao = new DicParamDao();
		int i = dao.delDicParam(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}
	
	public int modDicParam(Map<String, Object> values, Map<String, Object> cdt){
		DicParamDao dao = new DicParamDao();
		int i = dao.modDicParam(values, cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}
	
	public DicParam queryDicParam(int id){
		DicParamDao dao = new DicParamDao();
		DicParam sys = dao.queryDicParam(id);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return sys;
	}
	
	public int querySysParamitem(Map<String, Object> cdt){
		DicParamDao dao = new DicParamDao();
		int i = dao.queryDicParam(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}
	
	public List<DicParam> querySysParamitemList(Map<String, Object> cdt, int page, int pagesize){
		DicParamDao dao = new DicParamDao();
		List<DicParam> list = dao.queryDicParamList(cdt, page, pagesize);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return list;
	}

	public DicParam qryDicParams(Map<String, Object> cdt){
		DicParamDao dao = new DicParamDao();
		DicParam sys = dao.qryDicParams(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return sys;
	}
}